package top.kaoshanji.p2ch15b;

/**
 * 代码清单-p2ch15b-465页a：线程处于WAITING状态对中断的反应
 * author kaoshanji
 * ^_^
 * create time 2025/2/24 18:58
 */
public class InterruptWaitingDemo extends Thread {

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                // 模拟任务代码
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // ... 清理操作
                // 重设中断标志位
                Thread.currentThread().interrupt();
            }
        }
        System.out.println(isInterrupted());
    }

    public static void main(String[] args) {

        InterruptWaitingDemo demo = new InterruptWaitingDemo();
        demo.start();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }

        demo.interrupt();
    }

}
